<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<meta http-equiv="pragma" content="no-cache" />
	<link rel="stylesheet" href="/css/style.css" type="text/css"/>
	<script type="text/javascript" src="/js/jquery.js"></script>
	<script type="text/javascript" src="/js/util.js"></script>
</head>
<script type="text/javascript">

var arguments = window.dialogArguments;
var elementname = arguments[0];//Item
var elementdesc = arguments[1];//道具
var elementsstr = arguments[2];//currentvalues
window.resultValue = elementsstr;
var elements = [];
var count = 0;
if(elementsstr != undefined && elementsstr != "") {
	elements = s2json(elementsstr);
	count = elements.length;
}
$(document).ready(function() {
	var slices = "";
	for(var i=0;i<count;i++){
		var element = elements[i];
		slices = slices + getSlice(i,element);
	}
	$("#lists").html(slices);
	showElementNames2();
});
function showElementNames() {
	var getSelectNameUrl = "/special/" + elementname + "/get/";
	for(var i=0;i<count;i++){
		$.ajax({
			   type: "GET",
			   url: getSelectNameUrl,
			   data: "index=" + i + "&elementId=" + elements[i],
			   dataType: "json",
			   success: function(data) {
					if(data.code > 0) {
						var index = data.result[0];
						var elementname = data.result[1];
						$("#display" + index).html(elementname);
					}else {
						alert(data.message);
					}
				}
		});
	}
}
function showElementNames2() {
	var getSelectorNamesUrl = "/special/" + elementname + "/getList/";
	$.ajax({
		   type: "GET",
		   url: getSelectorNamesUrl,
		   data: "",
		   dataType: "json",
		   success: function(data) {
				if(data.code > 0) {
					elementNameList = s2json(data.result);
					for(var i=0;i<count;i++) {
						var elementName = "未选择";
						for(var j=0;j<elementNameList.length;j++) {
							if(elements[i] == elementNameList[j][0]) {
								elementName = elementNameList[j][1];
								break;
							}
						}
						$("#display" + i).html(elementName);
					}
				}else {
					alert(data.message);
				}
			}
	});
}
function getSlice(i,element,display) {
	if(display == undefined) {
		display = "";
	}
	var slice = "<div id='slice"+ i +"'>";
	slice = slice + "<input type='text' id='element"+ i +"' name='element"+ i + "' value='" + element + "' disabled/>";
	slice = slice + "<span id='display"+ i +"' name='display"+ i + "'>"+ display +"</span>";
	slice = slice + "<input type='button' value='...' onclick='editElement("+ i +")'/>";
	slice = slice + "<input type='button' value='删除' onclick='deleteElement("+ i +")'/>";
	slice = slice + "</div>";
	return slice;
}
function addElement() {
	var returnCallback = function(elementstr) {
		if(elementstr == null) {return;}
		element = parseInt(elementstr[0]);
		display = elementstr[1];
		slice = getSlice(count,element,display);
		$('#lists').html($('#lists').html() + slice);
		elements[count] = element;
		count = count + 1;
	}
	openDialog("/pages/core/Selector.html",[elementname,elementdesc,null],"dialogWidth:200px;dialogHeight:200px;status:no;help:no;center:yes",returnCallback);
}
function editElement(id) {
	var element = elements[id];
	var returnCallback = function(elementstr) {
		if(elementstr == null){return;}
		element = parseInt(elementstr[0]);
		display = elementstr[1];
		if(isNaN(element)) {
			alert("输入非法");
			return;
		}
		elements[id] = element;
		$("#element" + id).val(element);
		$("#display" + id).html(display);
	}
	openDialog("/pages/core/Selector.html",[elementname,elementdesc,element],"dialogWidth:200px;dialogHeight:200px;status:no;help:no;center:yes",returnCallback);
}
function deleteElement(id) {
	$('#slice' + id).hide();
	elements[id] = null;
}
function clearElements() {
	elements = []
	count = 0;
	$("#lists").html("");
}
function submitList() {
	var finalElements = [];
	for(var i=0;i<elements.length;i++) {
		if(elements[i] != undefined) {
			finalElements.push(elements[i]);
		}
	}
	window.returnValue = json2s(finalElements);
	beforeWindowClose();
	window.close();
}
function beforeWindowClose() {
	if(window.returnCallback) {
		window.returnCallback(window.returnValue);
	}
	window.onbeforeunload = null;
	return true;
}
</script>

<body onunload="beforeWindowClose">
	<div id="lists">
		
	</div>
	<input type="button" onclick="addElement()" value="增加元素"/>
	<input type="button" onclick="clearElements()" value="清空元素"/>
	<input type="button" onclick="submitList()" value = "提交"/>
</body>